/************************************************************************************
 *                                                                                  *
 *                                                                                  *
 ************************************************************************************/
package jMiiEditor.deprecated.swing;


import jMiiEditor.mii.Mii;
import java.awt.Component;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileSystemView;

/**
 * @author CC
 * TODO: to slow! try to use Tread for creating JFileChooser in background, after frame initialization
 */
@Deprecated
public class SwingStuffs {

    private static File openDefaultDirFile = null;
    private static File saveDefaultDirFile = null;
    private static JFileChooser openDefaultJFileChooser = null;
    private static JFileChooser saveDefaultJFileChooser = null;

    /*
    * Unregisters the zipfldr.dll fixes JDialog slowness
    * issues. (Should be made optional)
    */
    public static void applyFileChooserTweak()
    {
        String osName = System.getProperty( "os.name" ).toLowerCase();
        if( !osName.contains( "windows" ) ) return;
        try {
            System.out.print( "Unregistering zipfldr.dll to speed up program (don't worry, windows will reset this)..." );
            Runtime.getRuntime().exec( "regsvr32 /s %windir%/system32/zipfldr.dll" );
            System.out.println( "success" );
        }
        catch( Exception e ) {
            System.out.println( "failed" );
        }
    }

    public static JFileChooser getJFileChooser( final File defaultDirFile )
    {
        JFileChooser chooser =  new JFileChooser( defaultDirFile, FileSystemView.getFileSystemView() );

        // workaround: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6317789
        chooser.putClientProperty("FileChooser.useShellFolder", Boolean.FALSE);

        return chooser;
    }

    public static File chooseFile(
        final JFileChooser  chooser,
        final FileFilter    filter,
        final Component     parent
        )
    {
        chooser.setFileFilter(filter);

        int returnVal = chooser.showOpenDialog( parent );

        if(returnVal == JFileChooser.APPROVE_OPTION) {
            File file = chooser.getSelectedFile();

            System.out.println("You chose to open this file: " + file.getName());

            return file;
        }

        return null; // no file
    }

    public static File chooseMiiFile(
        final JFileChooser  openDefaultJFileChooser,
        final Component     parent
        )
    {
        final String    extensionDescription = "Mii files";
        final String[]  extensions = { "mii", "mae" };
        FileFilter      filter;

        // JDK >= 1.6
        //filter = new FileNameExtensionFilter( extensionDescription, extensions );
        filter = new FileFilter() {

                @Override
                public boolean accept( File file )
                {
                    for( String s : extensions ) {
                        if( file.getName().toLowerCase().endsWith( "." + s ) ) {
                            return true;
                        }
                    }
                    return false;
                }

                @Override
                public String getDescription()
                {
                    return extensionDescription;
                }

            };


        return chooseFile(
                        openDefaultJFileChooser,
                        filter,
                        parent
                        );
    }

    public static Mii openMiiFile( final Component parent ) throws FileNotFoundException, IOException
    {
        if( openDefaultDirFile == null ) {
            openDefaultDirFile = saveDefaultDirFile == null ? new File( "." ) : saveDefaultDirFile;
        }
        if( openDefaultJFileChooser == null ) {
            openDefaultJFileChooser = getJFileChooser( openDefaultDirFile );
        }

        File file = chooseMiiFile( openDefaultJFileChooser, parent );

        if( file != null ) {
            openDefaultDirFile = file.getParentFile();

            Mii mii = new Mii( file );

            return mii;
            }

        return null;
    }

    public static void saveMiiFile( final Mii mii, final Component parent ) throws FileNotFoundException, IOException
    {
        if( saveDefaultDirFile == null ) {
            saveDefaultDirFile = openDefaultDirFile == null ? new File( "." ) : openDefaultDirFile;
        }
        if( saveDefaultJFileChooser == null ) {
            saveDefaultJFileChooser = getJFileChooser( saveDefaultDirFile );
        }

        File file = chooseMiiFile( saveDefaultJFileChooser, parent );

        if( file != null ) {
            SwingStuffs.saveDefaultDirFile = file.getParentFile();

            mii.save( file );
            }
    }

    public static void setInvertSelection( javax.swing.AbstractButton from, javax.swing.AbstractButton to )
    {
        to.setSelected( !from.isSelected() );
    }

}
